﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;


using ThoiTrang.Business; 
using ThoiTrang.Entities;

public partial class Admin_adm_articleDet_upload : System.Web.UI.Page
{
    public string ckid = "";
    public string aid = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            a_Session ss = null;
            a_User usr = null;
            a_UserType usrType = null;
            ss = a_SessionService.Geta_Session(SessionManager.GetCookie());
            ckid = ss.SessionID;
            usr = a_UserService.Geta_User(ss.CookieID);
            usrType = a_UserTypeService.Geta_UserType(usr.UserTypeID);

            aid = Request.QueryString["aid"];
            loadYear();
            loadCountry();

            a_Article aa = a_ArticleService.Geta_Article(Convert.ToInt32(aid));
            ddlCountry.SelectedValue = aa.CountryID.ToString();
            ddlYear.SelectedValue = aa.FYear.ToString();
            ltrTitle.Text = aa.UTitle;
            string iid = Request.QueryString["iid"] == null ? "" : Request.QueryString["iid"];
            btnSaveEdit.Visible = false;
            if (iid != "")
            {
                btnSaveEdit.Visible = true;
            }
            lstWearType.SelectedIndex = 0;
            loadData();
        }
    }

    void loadData()
    { 
        aid = Request.QueryString["aid"];
        DataSet ds = a_ArticleService.a_articleUploadDet_up("", Convert.ToInt32(aid), "",
         ddlCountry.SelectedValue , 0, 0, 0, 0, "", "0", "0", 0);

        lstWearType.DataTextField = "WearTypeNameVN";
        lstWearType.DataValueField = "WTypeID";
        lstWearType.DataSource=ds.Tables[4];
        lstWearType.DataBind();

        dtlImage.DataSource = ds.Tables[ds.Tables.Count-1];
        dtlImage.DataBind();
    }

    void loadYear()
    {
        for (DateTime dt = Convert.ToDateTime("01/01/2006"); dt <= DateTime.Now.AddYears(1);dt= dt.AddYears(1))
        {
            ddlYear.Items.Add(new ListItem(dt.Year.ToString(), dt.Year.ToString()));
        }
    }
    void loadCountry()
    {
        ddlCountry.DataTextField = a_CountryColumns.CountryNameVN.ToString();
        ddlCountry.DataValueField = a_CountryColumns.CountryID.ToString(); ;
        ddlCountry.DataSource = ThoiTrang.Business.a_CountryService.Geta_CountryList(a_CountryColumns.CountryNameVN, ThoiTrang.Data.OrderDirection.ASC);
        ddlCountry.DataBind();
    }

    public string DetailImage(object aID, object kTitle, object fYear, object imgNo)
    {
        string res = "";
        res = kTitle.ToString().ToLower().Trim().Replace(" ", "-");
        res = FOConstants.Dir_ImgArticle + res + "-" + fYear.ToString() + "-d" + aID.ToString() + imgNo.ToString() + ".jpg";
        return res;
    }
    public string DetailImageName(object aID, object kTitle, object fYear, object imgNo)
    {
        string res = "";
        res = kTitle.ToString().ToLower().Trim().Replace(" ", "-");
        res =  res + "-" + fYear.ToString() + "-d" + aID.ToString() + imgNo.ToString() + ".jpg";
        return res;
    }
   
    protected void btnDelete_Click(object sender, ImageClickEventArgs e)
    {

    }
    protected void btnEdit_Click(object sender, ImageClickEventArgs e)
    {
        aid = Request.QueryString["aid"];
        ckid = Request.QueryString["ckid"];


    }
    protected void dtlImage_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            aid = Request.QueryString["aid"];
            DataSet ds = a_ArticleService.a_articleUploadDet_up("del", Convert.ToInt32(aid), "",
                  ddlCountry.SelectedValue, 0, 0, 0, 0, "", "0", "0", 
                  Convert.ToInt32(e.CommandArgument) );
        }
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        aid = Request.QueryString["aid"];
        a_Article aa = a_ArticleService.Geta_Article(Convert.ToInt32(aid));

        //last image number
        a_Articles_ImgCollection aimg = a_Articles_ImgService.Geta_Articles_ImgListByAID(aa.ID, a_Articles_ImgColumns.ImageNo, ThoiTrang.Data.OrderDirection.DESC);
        int maxImgNo = 0;
        try { maxImgNo = aimg[0].ImageNo; }
        catch { }

        int imgW = 0; ;
        //HTMLHelper.ResizeImageFile
        if (fluImage.PostedFile != null)
        {
            if (fluImage.PostedFile.FileName != "")
            {
                if (fluImage.PostedFile.ContentLength > 0)
                {
                    char[] spl = { '/', '\\' };
                    string fileServer = this.DetailImageName((object)aid, (object)aa.KTitle, (object)ddlYear.SelectedValue, maxImgNo + 1);
                    string dirServer = Server.MapPath("~/" + FOConstants.Dir_ImgArticle).Trim(spl);
                    try
                    {
                        System.IO.File.Delete(dirServer + "\\" + fileServer);
                    }
                    catch { }
                    fluImage.SaveAs(dirServer + "\\" + fileServer);
                    try
                    {// image width
                        System.Drawing.Image img = System.Drawing.Image.FromFile(dirServer + "\\" + fileServer);
                        imgW = img.Width;
                    }
                    catch { }
                }
            }
        }

        DataSet ds = a_ArticleService.a_articleUploadDet_up("add",
            Convert.ToInt32(aid), txtImageTitle.Text,
                ddlCountry.SelectedValue, Convert.ToInt32(ddlYear.SelectedValue),
                Convert.ToInt32(ddlSeason.SelectedValue), Convert.ToInt32(lstWearType.SelectedValue),
                Convert.ToInt32(ddlSex.SelectedValue), txtImageTitle.Text,
                ddlOrientedImage.SelectedValue 
                , imgW.ToString(), 0);
    }
    protected void btnMakeMainImage_Click(object sender, EventArgs e)
    {
        //rdbSelect

        //ltrImageID
        string iid = Request.QueryString["iid"] == null ? "" : Request.QueryString["iid"];  
        aid = Request.QueryString["aid"];
        if (iid != "")
        {
            DataSet ds = a_ArticleService.a_articleUploadDet_up("makemain", Convert.ToInt32(aid), "",
                       ddlCountry.SelectedValue, 0, 0, 0, 0, "", "0", "0",
                       Convert.ToInt32(iid));
        }
        else
        {
            foreach (DataListItem itm in dtlImage.Items)
            {
                RadioButton rdb = (RadioButton)itm.FindControl("rdbSelect");
                if (rdb.Checked)
                {
                    Literal ltr = (Literal)itm.FindControl("ltrImageID");
                    DataSet ds = a_ArticleService.a_articleUploadDet_up("makemain", Convert.ToInt32(aid), "",
                            ddlCountry.SelectedValue, 0, 0, 0, 0, "", "0", "0",
                           Convert.ToInt32(ltr.Text) );
                }
            }
        }

    }
    protected void btnSaveEdit_Click(object sender, EventArgs e)
    {
        string iid = Request.QueryString["iid"] == null ? "" : Request.QueryString["iid"];
        if (iid == "") return;
       
        if (iid != "")
        {
            aid = Request.QueryString["aid"];
            a_Article aa = a_ArticleService.Geta_Article(Convert.ToInt32(aid));

            //last image number
            a_Articles_ImgCollection aimg = a_Articles_ImgService.Geta_Articles_ImgListByAID(aa.ID, a_Articles_ImgColumns.ImageNo, ThoiTrang.Data.OrderDirection.DESC);
            int maxImgNo = 0;
            try { maxImgNo = aimg[0].ImageNo; }
            catch { }

            int imgW = 0; ;
            //HTMLHelper.ResizeImageFile
            if (fluImage.PostedFile != null)
            {
                if (fluImage.PostedFile.FileName != "")
                {
                    if (fluImage.PostedFile.ContentLength > 0)
                    {
                        char[] spl = { '/', '\\' };
                        string fileServer = Request.QueryString["imgName"];
                            // this.DetailImageName((object)aid, (object)aa.KTitle, (object)ddlYear.SelectedValue, maxImgNo + 1);
                        string dirServer = Server.MapPath("~/" + FOConstants.Dir_ImgArticle).Trim(spl);
                        try
                        {
                            System.IO.File.Delete(dirServer + "\\" + fileServer);
                        }
                        catch { }
                        fluImage.SaveAs(dirServer + "\\" + fileServer);
                        try
                        {// image width
                            System.Drawing.Image img = System.Drawing.Image.FromFile(dirServer + "\\" + fileServer);
                            imgW = img.Width;
                        }
                        catch { }
                    }
                }
            }

            DataSet ds = a_ArticleService.a_articleUploadDet_up("upd",
                Convert.ToInt32(aid), txtImageTitle.Text,
                    ddlCountry.SelectedValue, Convert.ToInt32(ddlYear.SelectedValue),
                    Convert.ToInt32(ddlSeason.SelectedValue), Convert.ToInt32(lstWearType.SelectedValue),
                    Convert.ToInt32(ddlSex.SelectedValue), txtImageTitle.Text, ddlOrientedImage.SelectedValue
                    , imgW.ToString(),Convert.ToInt32( iid));
       
        }
    }
    protected void btnMakeFeatured_Click(object sender, EventArgs e)
    {
        aid = Request.QueryString["aid"];
     
        a_Article aa = a_ArticleService.Geta_Article(int.Parse(aid));
        aa.IsFeatured = true;
        a_ArticleService.Updatea_Article(aa);
    }
}
